package com.example.imchat.controller.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.example.imchat.R;
import com.example.imchat.controller.adapter.PcikContactAdapter;
import com.example.imchat.model.Model;
import com.example.imchat.model.bean.PickContactInfo;
import com.example.imchat.model.bean.UserInfo;
import com.example.imchat.utils.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;

import java.util.ArrayList;
import java.util.List;

public class PickContactActivity extends Activity {
    private TextView tv_pick_save;
    private ListView lv_pick;
    private PcikContactAdapter pcikContactAdapter;
    private   List<PickContactInfo> mPicks;
    private  List<String> mExistMembers;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_contact);

        initView();
        initData();
        getData();
        initListener();
    }

    private void getData() {
        String groupId = getIntent().getStringExtra(Constant.GROUP_ID);
        if (groupId != null){
            EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
            //获取已经存在的所有群成员
            mExistMembers = group.getMembers();
        }
        if (null == mExistMembers){
            mExistMembers = new ArrayList<>();

        }

    }

    private void initListener() {
        //listview条目的点击事件
        lv_pick.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //checkbox点击的切换
                CheckBox cb_pick = view.findViewById(R.id.cb_pick);
                cb_pick.setChecked(!cb_pick.isChecked());

                //修改数据
                PickContactInfo pickContactInfo = mPicks.get(position);
                pickContactInfo.setChecked(cb_pick.isChecked());

                //刷新页面
                pcikContactAdapter.notifyDataSetChanged();
            }
        });

        //保存按钮的事件
        tv_pick_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取选择的联系人
                List<String> names =  pcikContactAdapter.getPickContacts();
                //给启动页面返回数据
                Intent intent = new Intent();
                //list转换为string数组
                intent.putExtra("members",names.toArray(new String[0]));
                //设置返回结果码
                setResult(RESULT_OK,intent);
                //结束当前页面
                finish();
            }
        });
    }

    private void initData() {
        //从本地数据库中获取联系人
        List<UserInfo> contacts = Model.getInstance().getDbManager().getContactTableDao().getContacts();
        mPicks = new ArrayList<>();
        if (null != contacts && contacts.size()>=0){
            //转换
            for (UserInfo contact :contacts){
                PickContactInfo pickContactInfo = new PickContactInfo(contact, false);
                mPicks.add(pickContactInfo);
            }
        }

        //初始化listview
        pcikContactAdapter = new PcikContactAdapter(this,mPicks,mExistMembers);
        lv_pick.setAdapter(pcikContactAdapter);
    }

    private void initView() {
        tv_pick_save = findViewById(R.id.tv_pick_save);
        lv_pick = findViewById(R.id.lv_pick);
    }
}
